function initial() {
    addPathSelect("#csvFileChoose", "csvpathSelect");
}

function bindRelationFile() {

    bindCheckBox($(".relation-file"));

}

function bindCheckBox(group) {
    group.click(function() {
	var $box = $(this);
	if ($box.is(":checked")) {
	    group.prop("checked", false);
	    $box.prop("checked", true);
	} else {
	    $box.prop("checked", false);
	}
    })
}

function viewFileFromFolder() {
    $("#view-btn").click(function() {
		$.ajax({
		    url : "../rest/relation/getFilesFromFolder",
		    data : {
			"path" : $("#csvpathSelect").val()
		    },
		    method : "POST",
		    dataType : "json",
		    success : function(data) {
			$("#viewBody").html("");
			$.each(data, function(k, v) {
			    $("#viewBody").append("<tr><td>" + v + "</td><td><input type='checkbox' class='relation-file' value=" + k + " /></td></tr>");
			    bindRelationFile();
			});
		    }
		    
		})
		
    })
}

function FilterClick() {
    $("#filter-btn").click(function() {
	if ($(".relation-file:checked").length == 1) {
	    $("#filterPath").val($(".relation-file:checked").val());
	    $("#filterForm").submit()
	} else {
	    alert("please select a file");
	}
    });
}

function mappingClick() {
    $("#mapping-btn").click(function() {

	if ($(".relation-file:checked").length == 1) {
	    $("#functionPath").val($(".relation-file:checked").val());

	    $("#functionForm").submit()
	} else {
	    alert("please select a file");
	}

    })
}

$(function() {
    initial();
    viewFileFromFolder();
    FilterClick();
    mappingClick();
})